package com.zhuang.chapter16.work03;

public class PrintMain {

	public static void main(String[] args) {
		// 创建锁对象
		final Object object = new Object();

		// 创建线程A,开启线程
		new Thread(new Runnable() {

			@Override
			public void run() {
				for (int i = 1; i <= 52; i++) {
					synchronized (object) {
						try {
							System.out.println(i);
							// 进入等待状态,并释放锁
							object.wait();// 无线等待状态
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}
			}
		}, "A").start();

		// 创建线程B
		new Thread(new Runnable() {

			@Override
			public void run() {
				int count = 0;
				while (count < 52) {
					for (char i = 'A'; i <= 'Z'; i++) {
						synchronized (object) {
							System.out.println(i);
							// 唤醒线程A
							object.notify();
							count++;
						}
						// 线程A和线程B交换执行关键所在
						try {
							Thread.sleep(10);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}, "B").start();
	}

}
